//
//  SendMessageView.m
//  CarPlateInputViewDemo
//
//  Created by ilongge on 2021/2/2.
//

#import "SendMessageView.h"

@interface SendMessageView()
<
UITextViewDelegate
>
@property (weak, nonatomic) IBOutlet UITextView *messageTextView;
@property (weak, nonatomic) IBOutlet UIButton *sendMessageButton;
@property (weak, nonatomic) IBOutlet UIButton *selectEmojiButton;
@property (nonatomic, assign) BOOL shouldPop;
@end

@implementation SendMessageView

+ (instancetype)sendMessageView{
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSString *classString = [[self class] description];
    id view = [[bundle loadNibNamed:classString
                              owner:self
                            options:nil] firstObject];
    return view;
}
-(void)setOrginFrame:(CGRect)orginFrame{
    _orginFrame = orginFrame;
    self.frame = orginFrame;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    self.clipsToBounds = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textViewTextDidChange)
                                                 name:UITextViewTextDidChangeNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];
    self.shouldPop = YES;
}

- (void)keyboardWillShow:(NSNotification*)aNotification{
    if (self.isEditing == YES && self.shouldPop == YES) {
        CGRect rect = _orginFrame;
        NSDictionary *keyBordInfo = [aNotification userInfo];
        NSValue *value = [keyBordInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyBoardRect = [value CGRectValue];
        rect.origin.y = keyBoardRect.origin.y-rect.size.height;
        self.frame = rect;
        self.shouldPop = NO;
    }
}

- (void)keyboardDidHide:(NSNotification*)aNotification{
    
    self.frame = _orginFrame;
    self.shouldPop = YES;
}

- (IBAction)selectEmojiAction:(id)sender {
    
}

- (IBAction)senMessageAction:(id)sender {
    if (self.sendMessageBlock && self.messageTextView.text.length) {
        self.sendMessageBlock(self.messageTextView.text);
    }
}

- (void)textViewTextDidChange{
    NSString *message = self.messageTextView.text;
    CGFloat height = [message boundingRectWithSize:CGSizeMake(self.frame.size.width, 0)
                                           options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                        attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}
                                           context:nil].size.height;
    if (height<30) {
        height = 30;
    }
    CGRect frame = self.frame;
    frame.size.height = height+10;
    frame.origin.y = frame.origin.y - height-10;
    self.orginFrame = frame;
    [self layoutIfNeeded];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    return YES;
}

@end
